package controllers;

import helpers.ConsoleUtils;

import java.util.Date;
import java.util.List;

import org.apache.commons.io.FilenameUtils;
import org.codehaus.jackson.node.ObjectNode;

import be.objectify.deadbolt.java.actions.Group;
import be.objectify.deadbolt.java.actions.Restrict;

import models.Resource;
import models.ResourceType;
import play.data.DynamicForm;
import play.data.Form;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import services.TestResourceService;
import views.html.resources.*;

public class TestResources extends Controller {

	private static final TestResourceService resourceService = TestResourceService.getService();

	@Restrict(@Group(Application.ROLE_USER))
	public static Result listResources() {
		List<Resource> testResources = resourceService.getAllResources();
		return ok(list.render(testResources));
	}

	@Restrict(@Group(Application.ROLE_USER))
	public static Result addResource() {
		Form<Resource> form = new Form<Resource>(Resource.class);

		form.data().put("define-resource-file-name", "");
		form.data().put("resource-file-name", "");
		form.data().put("resource-content", "");

		return ok(add.render(form));
	}

	@Restrict(@Group(Application.ROLE_USER))
	public static Result editResource(long resourceId) {
		Resource testResource = resourceService.getResource(resourceId);
		if (testResource != null) {
			Form<Resource> form = Form.form(Resource.class).fill(testResource);

			String stringResource = resourceService.getResourceFileContent(testResource);
			String resourceFileName = FilenameUtils.getBaseName(testResource.getResourcePath());

			form.data().put("define-resource-file-name", "");
			form.data().put("resource-file-name", resourceFileName);
			form.data().put("original-resource-file-name", resourceFileName);
			form.data().put("resource-content", stringResource);

			return ok(edit.render(testResource, form));
		} else {
			return TODO;
		}
	}

	@Restrict(@Group(Application.ROLE_USER))
	public static Result createResource() {
		Form<Resource> form = Form.form(Resource.class).bindFromRequest();
		if (!form.hasErrors()) {
			Resource testResource = form.get();
			testResource.setCompany(ConsoleUtils.getCompany());
			testResource.setName(testResource.getName().trim());
			testResource.setType(ResourceType.TestResource);
			testResource.setCreatedDate(new Date());
			testResource.setLastModifiedDate(testResource.getCreatedDate());

			DynamicForm requestForm = Form.form().bindFromRequest();
			String defineResourceFileName = requestForm.get("define-resource-file-name");
			String resourceFileName = null;
			if (defineResourceFileName != null && defineResourceFileName.equalsIgnoreCase("on")) {
				resourceFileName = requestForm.get("resource-file-name");
			}
			String resourceContent = requestForm.get("resource-content");

			resourceService.saveResource(testResource.getCompany(), testResource, resourceFileName, resourceContent);

			return redirect(controllers.routes.TestResources.listResources());
		} else {
			DynamicForm requestForm = Form.form().bindFromRequest();

			String defineResourceFileName = requestForm.get("define-resource-file-name");
			if (defineResourceFileName != null && defineResourceFileName.equalsIgnoreCase("on")) {
				form.data().put("define-resource-file-name", "checked");
			} else {
				form.data().put("define-resource-file-name", "");
			}

			return ok(add.render(form));
		}
	}

	@Restrict(@Group(Application.ROLE_USER))
	public static Result updateResource(long testResourceId) {
		Resource testResource = resourceService.getResource(testResourceId);
		Form<Resource> form = Form.form(Resource.class).bindFromRequest();
		if (!form.hasErrors()) {
			Resource newConfig = form.get();
			testResource.setName(newConfig.getName().trim());
			testResource.setDescription(newConfig.getDescription());
			testResource.setLastModifiedDate(new Date());

			DynamicForm requestForm = Form.form().bindFromRequest();
			String defineResourceFileName = requestForm.get("define-resource-file-name");
			String resourceFileName = null;
			if (defineResourceFileName != null && defineResourceFileName.equalsIgnoreCase("on")) {
				resourceFileName = requestForm.get("resource-file-name");
			}
			String resourceContent = requestForm.get("resource-content");

			if (testResource.getProject() != null) {
				resourceService.saveResource(testResource.getProject(), testResource, resourceFileName, resourceContent);
			} else {
				resourceService.saveResource(testResource.getCompany(), testResource, resourceFileName, resourceContent);
			}
			return redirect(controllers.routes.TestResources.editResource(testResourceId));
		} else {
			DynamicForm requestForm = Form.form().bindFromRequest();

			String defineResourceFileName = requestForm.get("define-resource-file-name");
			if (defineResourceFileName != null && defineResourceFileName.equalsIgnoreCase("on")) {
				form.data().put("define-resource-file-name", "checked");
			} else {
				form.data().put("define-resource-file-name", "");
			}

			return ok(edit.render(testResource, form));
		}
	}

	@Restrict(@Group(Application.ROLE_USER))
	public static Result getResourceContentRaw(long testResourceId) {
		ObjectNode result = Json.newObject();
		if (resourceService.resourceExists(testResourceId)) {
			Resource testResource = resourceService.getResource(testResourceId);
			result.put("content", resourceService.getResourceFileContent(testResource));
			return ok(result);
		} else {
			result.put("content", "");
			return notFound(result);
		}
	}

}
